package com.lqq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lqq.entity.Employee;
import com.lqq.entity.Msg;
import com.lqq.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lqq on 2017/6/28.
 */
@Controller
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @RequestMapping("/index")
    public String index(@RequestParam(value = "curPage",defaultValue ="1") Integer curPage, Model model){

        PageHelper.startPage(curPage,5);
        List<Employee> employees = employeeService.getAllEmps();
        PageInfo page = new PageInfo(employees,5);
        model.addAttribute("pageInfo",page);
        return "list";
    }

    @RequestMapping("/indexWithJson")
    @ResponseBody
    public Msg indexWithJson(@RequestParam(value = "curPage",defaultValue ="1") Integer curPage){
        PageHelper.startPage(curPage,5);
        List<Employee> employees = employeeService.getAllEmps();
        PageInfo page = new PageInfo(employees,5);
        return Msg.success().add("pageInfo",page);
    }

    //添加员工
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    @ResponseBody
    public Msg add(@Valid  Employee employee, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            Map<String,Object> map = new HashMap<>();
            for(FieldError fieldError : fieldErrors){
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            return Msg.fail().add("fieldErrors",map);
        }
        employeeService.addEmp(employee);
        return Msg.success();
    }

    @RequestMapping("/emp/checkUser")
    @ResponseBody
    public Msg checkUser(@RequestParam("empName") String empName){
        String regex = "^([a-z0-9_-]{6,16})|([\\u2E80-\\u9FFF]+)$";
        if(!empName.matches(regex)){
            return Msg.fail().add("error_name","用户名由6-16位字母和数组组合或中文");
        }
        if(employeeService.checkUser(empName)){
            return Msg.success();
        }else{
            return Msg.fail().add("error_name","用户名已存在!");
        }
    }

    //获取员工
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmployee(@PathVariable("id") int id){
       Employee employee = employeeService.getEmployee(id);
       return Msg.success().add("employee",employee);
    }

    //修改员工
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateEmployee(@Valid  Employee employee,BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            Map<String,Object> map = new HashMap<>();
            for(FieldError fieldError : fieldErrors){
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            return Msg.fail().add("fieldErrors",map);
        }
        employeeService.updateEmployee(employee);
        return Msg.success();
    }

    //删除员工
    /*
       单一删除和批量删除二合一
     */
    @RequestMapping(value = "/emp/{ids}",method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteEmployee(@PathVariable("ids") String ids){
        if(ids.contains("-")){
            List<Integer> list = new ArrayList<>();
            String[] split = ids.split("-");
            for(String s : split){
                list.add(Integer.parseInt(s));
            }
            employeeService.deleteBatch(list);
        }else{
            employeeService.deleteEmployee(Integer.parseInt(ids));

        }
        return Msg.success();
    }
}
